package com.xinxing.city.recovery.hk;

import android.content.Context;
import android.util.Log;
import android.widget.Toast;

import com.hikvision.netsdk.HCNetSDK;
import com.xinxing.city.recovery.Constant;

public class Hk {

    private String userip;
    private String username;
    private String userpassward;
    private String port;
    private int lUserID;
    private Context cont;
    public Hk(Context context,String userIp,String userName,String userPassward,String por) {
        cont = context;

        userip = userIp;
        username = userName;
        userpassward = userPassward;
        port = por;

        if (!HCNetSDK.getInstance().NET_DVR_Init()) {
            Constant.INIT_HK = false;
            Log.e("photo","摄像机SDK初始化失败");
            Toast.makeText(context, "摄像机SDK初始化失败", Toast.LENGTH_SHORT).show();
        }
    }

    //初始化
    public int  initVideo() {
        HCNetSDK.getInstance().NET_DVR_Init();
        HCNetSDKByJNA.NET_DVR_USER_LOGIN_INFO loginInfo = new HCNetSDKByJNA.NET_DVR_USER_LOGIN_INFO();

        System.arraycopy(userip.getBytes(), 0, loginInfo.sDeviceAddress, 0, userip.length());
        System.arraycopy(username.getBytes(), 0, loginInfo.sUserName, 0, username.length());
        System.arraycopy(userpassward.getBytes(), 0, loginInfo.sPassword, 0, userpassward.length());
        loginInfo.wPort = Short.parseShort(port);
        HCNetSDKByJNA.NET_DVR_DEVICEINFO_V40 deviceInfo = new HCNetSDKByJNA.NET_DVR_DEVICEINFO_V40();
        loginInfo.write();

        lUserID = HCNetSDKJNAInstance.getInstance().NET_DVR_Login_V40(loginInfo.getPointer(), deviceInfo.getPointer());

        if(lUserID==-1){
            Log.e("photo","无法连接到摄像头");
            Toast.makeText(cont, "无法连接到摄像头", Toast.LENGTH_SHORT).show();
            Log.e("错误码",HCNetSDKJNAInstance.getInstance().NET_DVR_GetLastError()+"");
        } else {
            Log.e("photo","摄像机初始化成功");
            Toast.makeText(cont, "摄像机初始化成功", Toast.LENGTH_SHORT).show();
        }
        return lUserID;
    }

}
